package com.example.red_tea_house.moudle.LoginAndRegister.controller;

import android.content.Context;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.annotation.UrlType;
import com.example.red_tea_house.entity.LoginBean;
import com.example.red_tea_house.moudle.LoginAndRegister.model.LegalData;
import com.example.red_tea_house.moudle.LoginAndRegister.view.RegisterActivity;
import com.example.red_tea_house.util.ToastUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import okhttp3.Call;



public class RegisterController extends GetVertifyController {
    private final String registerAccountUrl="register";

    private String registerReturnCode;

    private LoginBean loginBean=new LoginBean();
    private LegalData legalData=new LegalData();

    private final RegisterActivity registerActivity;

    Context context = MyApplication.getContext();

    public RegisterController(RegisterActivity registerActivity){
        this.registerActivity=registerActivity;
    }
    public boolean setRegisterInfo(String phone, String password1, String password2){
        loginBean.setPassword(password1);
        loginBean.setPhone(phone);
        if(legalData.isLegalData(loginBean,password2)){
            return true;
        }else {
            return false;
        }
    }

    public void registerAccount(){
        OkHttpUtils
                .get()
                .url(UrlType.REMOTESERVER_URL +registerAccountUrl+"?phone="+loginBean.getPhone()+"&password="+loginBean.getPassword())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.showToast(context,e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        registerReturnCode = gson.fromJson(response,String.class);
                        registerActivity.registerAndReturnLoginPage();
                        /*
                        * TODO:使用handler处理registerReturnCode,比如账号已注册等问题
                        * */
                    }
                });
    }

    @Override
    protected void vertifySucess(){
        registerAccount();
    }


}
